package com.sbeam.controller;

import com.sbeam.pojo.Games;
import com.sbeam.pojo.User;
import com.sbeam.service.UserService;
import com.sbeam.vo.SysResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        String token = userService.findByUP(user);
        if(token == null || "".equals(token)){
            return SysResult.fail();
        }
        return SysResult.success(token);
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }
    @PutMapping("/findUser")
    public SysResult findUser(@RequestBody User user){
        String token = userService.findUser(user);
        if(token != null){
        return SysResult.success();
    }
        return SysResult.fail();
}
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    @GetMapping("/getPersonInfo/{id}")
    public SysResult getPersonInfo(@PathVariable Integer id){
        User user = userService.getPersonInfo(id);
        return SysResult.success(user);
    }
    @GetMapping("/findUserByName")
    public SysResult findUserByName(String username){
        User user = userService.findUserByName(username);
        return SysResult.success(user);
    }
    @PutMapping("/updateUserById")//修改昵称，手机号
    public SysResult updateUserById(@RequestBody User user){
        userService.updateUser1(user);
        return SysResult.success();
    }
    @PutMapping("/updateUserPW")//修改密码
    public SysResult updateUserPW(@RequestBody User user){
        userService.updateUser2(user);
        return SysResult.success();
    }
    @GetMapping("/getOrderList")
    public SysResult getOrderList(String username){
        List<Games> games = userService.getOrderList(username);
        return SysResult.success(games);
    }
    @DeleteMapping("/deleteByName/{username}")
    public SysResult deleteByName(@PathVariable String username){
        userService.deleteByName(username);
        return SysResult.success();
    }
}
